package com.itheima.api;

import cn.hutool.Hutool;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.User;
import com.itheima.domain.UserInfo;
import com.itheima.mapper.UserInfoMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 保存用户信息
     * @param userInfo
     */
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    public void delete(UserInfo userInfo){
        userInfoMapper.deleteById(userInfo);
    }


    public void update(UserInfo userInfo){
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 查询用户信息
     * @param id
     * @return
     */
    @Override
    public UserInfo getById(Long id) {
        return userInfoMapper.selectById(id);
    }

    /**
     * 根据ids批量查询userInfo
     * 返回的时候返回map集合，方便操作数据
     * @param userIds
     * @return
     */
    @Override
    public Map<Long,UserInfo> getByIds(List<Long> userIds, UserInfo userInfo) {
        //把userInfo查出来
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UserInfo::getId,userIds);
//        List<UserInfo> userInfos1 = userInfoMapper.selectList(queryWrapper);
//        queryWrapper.eq(UserInfo::getId,6);
        if (userInfo != null){
            queryWrapper.lt(userInfo.getAge() != null,UserInfo::getAge,userInfo.getAge());
            queryWrapper.eq(userInfo.getGender() != null,UserInfo::getGender,userInfo.getGender());
            queryWrapper.like(userInfo.getNickname() != null,UserInfo::getNickname,userInfo.getNickname() );
        }
        List<UserInfo> userInfos = userInfoMapper.selectList(queryWrapper);
        //转换为Map
        Map<Long,UserInfo> map = new HashMap<>();
        for (UserInfo info : userInfos) {
            map.put(info.getId(),info);
        }
        return map;
    }

}
